package org.brownflat.watergate.domain;

import org.brownflat.watergate.scm.RevisionChange;
import org.brownflat.watergate.scm.RevisionChangeType;
import org.brownflat.watergate.scm.RevisionElementType;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * <p>&copy Rupert Jones 2012</p>
 *
 * @author rup
 */
@Component
public class CommitElementBuilder {

    public CommitElement build(Commit commit, String path) {
        CommitElement commitElement = new CommitElement();
        commitElement.setCommit(commit);
        commitElement.setPath(path);
        return commitElement;
    }

    public CommitElement build(Commit commit, RevisionChange revisionChange){
        CommitElement commitElement = new CommitElement();
        commitElement.setCommit(commit);
        commitElement.setPath(revisionChange.getPath());
        commitElement.setCreatedAt(new Date());

        if(RevisionChangeType.ADD.equals(revisionChange.getRevisionChangeType())) {
            commitElement.setCommitElementAction(CommitElementAction.ADD);
        } else if(RevisionChangeType.DELETE.equals(revisionChange.getRevisionChangeType())) {
            commitElement.setCommitElementAction(CommitElementAction.DELETE);
        } else if(RevisionChangeType.MODIFY.equals(revisionChange.getRevisionChangeType())) {
            commitElement.setCommitElementAction(CommitElementAction.MODIFY);
        } else if(RevisionChangeType.REPLACE.equals(revisionChange.getRevisionChangeType())) {
            commitElement.setCommitElementAction(CommitElementAction.REPLACE);
        } else  {
            commitElement.setCommitElementAction(CommitElementAction.UNKNOWN);
        }

        if(RevisionElementType.FILE.equals(revisionChange.getRevisionElementType())) {
            commitElement.setCommitElementType(CommitElementType.FILE);
        } else if(RevisionElementType.DIRECTORY.equals(revisionChange.getRevisionElementType())) {
            commitElement.setCommitElementType(CommitElementType.DIRECTORY);
        } else {
            commitElement.setCommitElementType(CommitElementType.UNKNOWN);
        }
        return commitElement;
    }
}
